const mixins = require('../../../mixins/forum')
const common = require('../../../mixins/common')
const app = getApp()
import config from "../../../api/config";
let interstitialAd = null;
const options = {

  /**
   * 页面的初始数据
   */
  data: {
   
    checkIndex: 0,
    currentIdx: 0,
    header: ["推荐", "关注"],
    placeholder: '搜索你感兴趣的内容',
    swiperload: true,
    banner: [],
    menu: config.menu,
    sticky: [],
    tags: [],
    cats: [],
    posts: [],
    advert: app.globalData.advert,
    innerAudioContext: null,
    audioCurrent: 0,
    page: 1,
    stickypage: 1,
    loading: false,
    popupshow: false,
    topicload: true,
    scrollTop: 5,
    isLastPage: false,
    currentConfig:config.currentConfig,
    audioInfo: {
      isPlaying: false,
      progress: 0
    },
    timer: null,
    innerAudioContext: null,
    showloading: false,
    videoindex: 0,
  },
  toDetail(){
    wx.redirectTo({   
     url: "/pages/mine/sign/sign"
   })
  },
  click(){
    wx.navigateTo({
      url: 'pages/mine/sign/sign?id=2',
    })
  } ,

  /**
   * 生命周期函数--监听页面加载
   */
  
 


  onLoad: function (options) {
    let that = this;
    if (wx.createInterstitialAd && this.data.advert.interstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId: this.data.advert.interstitialAd
      })
      interstitialAd.onLoad(() => {})
      interstitialAd.onError((err) => {})
      interstitialAd.onClose(() => {})
    }
    if (options.openId) {
      that.shareCredit(options.openId);
    }
    that.getBanners('wechat');
    that.getStickyLists();
    that.getTopicLists();
    that.getSiteInfos();
    that.getCircleLists({
      'custom': 'sticky'
    });
    that.getTagList({
      per_page: 12
    });
  },



  /**
   * *分享获取积分 */

  shareCredit: function (openId) {
    shared_user_id = wx.getStorageSync("share_user_id");
    if (openId && openId !== shared_user_id) {
      wx.setStorageSync("share_user_id", openId);
      var user = app.globalData.user;
      user ? this.updataShareCredit(openId, user.openId) : (user = "", setTimeout(function () {
        that.data.currentConfig.show = true;
        that.setData({
          currentConfig: that.data.currentConfig,
        })
      }, 2e3));
    }
  },


  /**
   * 轮播图切换
   */

  changeCurrentIdx: function (e) {
    this.setData({
      currentIdx: e.detail.current
    });
  },



  /**
   *  跳转搜索
   */

  gotoSearch: function () {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },



  /**
   * 跳转更多标签
   */

  moretags: function () {
    wx.navigateTo({
      url: '/pages/tags/list',
    })
  },



  /**
   *  跳转详情页
   */

  gotoDetail: function (e) {
    console.log(e);
    let id = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + id + '&type=forums',
    })
  },

  /**
   * 跳转更多精选内容
   */

  more: function () {
    wx.navigateTo({
      url: '/pages/sticky/sticky',
    })
  },

    /**
   * 跳转更多精选圈子
   */

  moreCircle: function (e) {
    wx.switchTab({
      url: '/pages/tabbar/circle/circle',
    })
  },




  /*
   *  轮播图点击事件
   */

  navToBannerLink: function (e) {
    let url = e.currentTarget.dataset.url;
    let type = e.currentTarget.dataset.type;
    if (type == 'page') {
      wx.navigateTo({
        url: url,
      })
    } else if (type == 'app') {
      wx.navigateToMiniProgram({
        appId: url,
      })
    }
  },


  /**
   * 菜单点击
   * @param {} e 
   */

  gridtap: function (e) {
    let url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    })

  },


  /**
   *切换导航栏
   */

  check: function (e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      checkIndex: index,
      posts: [],
      page: 1,
      topicNodate:false,
      isLastPage: false,
      topicload: true,
    })
    if (this.data.checkIndex == 1) {
      this.getFollowForum();
    } else {
      this.getTopicLists();
    }
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    if (interstitialAd) {
      interstitialAd.show().catch((err) => {
        console.error(err)
      })
    }
    this.initInnerAudioContext();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
    let user = app.globalData.user
    if (!user) {
      user = '';
    }
    this.setData({
      user: user,
    })

    if (this.data.tabIndex == 2) {
      this.getFollowArticle({
        page: this.data.page
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this;
    that.setData({
      posts: [],
      page: 1,
      sticky: [],
      isPull: true,
      topicload: true,
      topicNodate:false,
      isLastPage: false,
    })

    if (this.data.checkIndex == 0) {
      that.getStickyLists();
      this.getTopicLists({
        page: this.data.page,
      })
    } else {
      this.getFollowForum()
    }
    wx.stopPullDownRefresh({
      success: (res) => {},
    })

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      isBottom: true,
    })
    if (this.data.checkIndex == 0) {
      if (!this.data.isLastPage && !this.data.topicload) {
          this.getTopicLists({
            page: this.data.page,
          })
      }
    } else {
      this.getFollowForum({
        page: this.data.page,
      })
    }
  },





  /**
   * 取消
   */
  onClickCancle: function () {
    this.setData({
      showDialog: !this.data.showDialog
    })

  },
  /**
   * 显示删除菜单
   */

  editOrDeleteTap: function () {
    this.setData({
      showDialog: !this.data.showDialog,
      bounced: !this.data.bounced
    })
  },

  /**
   * 取消删除
   */

  bouncedTap: function () {
    this.setData({
      bounced: !this.data.bounced
    })
  },




  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    return {
      title: this.data.title,
      path: '/pages/tabbar/index/index',
      imageUrl: this.data.cover,
      success: (res) => {
        console.log("转发成功", res);
      },
      fail: (res) => {
        console.log("转发失败", res);
      }
    }
  },
  onShareTimeline: function (res) {
    return {
      title: this.data.title,
      query: '',
      imageUrl: this.data.cover //图片地址
    }
  }
}



mixins(options)
common(options)
Page(options)